//
//  XinCoffeeVC.swift
//  XinGuoSearch
//
//  Created by 李东 on 2024/4/8.
//

import UIKit
import SnapKit

class XinCoffeeVC: XinBaseVC {
    
    lazy var emptyImgView = UIImageView(image: UIImage(named: "img_coffee_empty"))
    lazy var tableView = UITableView(frame: .zero, style: .grouped)
    lazy var addBtn = UIButton(type: .custom)
    lazy var headerView = XinCoffeeHeaderView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 200))
    private let CellID = NSStringFromClass(XinCoffeeCell.self)
    lazy var dataList = [XinCoffeeModel]()

    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showPopBtn = true
        navTitle = "咖了么"
        initUI()
    }
    
    lazy var df: DateFormatter = {
        let temo = DateFormatter()
        temo.dateFormat = "yyyy-MM-dd"
        return temo
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        dataList = XinDataHelper.shared.getLocalList()
        emptyImgView.isHidden = !dataList.isEmpty
        tableView.isHidden = dataList.isEmpty

        headerView.reloadData(today: df.string(from: Date()))
        tableView.reloadData()
        if dataList.isEmpty {
            addBtn.setImage(UIImage(named: "img_coffee_drink"), for: .normal)
            addBtn.snp.updateConstraints { make in
                make.bottom.equalTo(-80)
            }
        }else{
            addBtn.setImage(UIImage(named: "img_coffee_add"), for: .normal)
            addBtn.snp.updateConstraints { make in
                make.bottom.equalTo(-30)
            }
        }
    }
    
    func initUI()  {
        addBtn.setImage(UIImage(named: "img_coffee_drink"), for: .normal)
        addBtn.addTarget(self, action: #selector(addCoffeeHandle), for: .touchUpInside)
        view.addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-30)
        }
        
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.backgroundColor = .clear
        tableView.dataSource = self
        tableView.rowHeight = 90
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.register(UINib(nibName: "XinCoffeeCell", bundle: nil), forCellReuseIdentifier: CellID)
        tableView.tableFooterView = UIView(frame: CGRect(x: 18, y: 20, width: CGFloat.screenWidth-36, height: 30))
        tableView.tableHeaderView = headerView
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(CGFloat.navHeight + 25)
            make.bottom.equalTo(addBtn.snp.top).offset(-20)
        }
        
        view.addSubview(emptyImgView)
        emptyImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(CGFloat.navHeight + 50)
        }
    }
    
    @objc func addCoffeeHandle() {
        navigationController?.pushViewController(XinAddCoffeeVC(), animated: true)
    }
   

}

extension XinCoffeeVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        dataList.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CellID, for: indexPath) as! XinCoffeeCell
        cell.model = dataList[indexPath.section]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        15
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        UIView()
    }
}
